from sqlalchemy import Column, String, DateTime, Boolean, ForeignKey
from sqlalchemy.orm import relationship
from datetime import datetime
from app.db.session import Base

class User(Base):
    __tablename__ = "users"

    id = Column(String(36), primary_key=True, index=True)
    email = Column(String(255), unique=True, index=True, nullable=False)
    nickname = Column(String(255))
    hashed_password = Column(String(255), nullable=False)
    avatar = Column(String(255))
    is_active = Column(Boolean, default=True)
    create_time = Column(DateTime, default=datetime.utcnow)
    last_login_time = Column(DateTime, default=datetime.utcnow)

    # 关系
    favorites = relationship("UserFavorite", back_populates="user")
    history = relationship("UserHistory", back_populates="user")

class UserFavorite(Base):
    __tablename__ = "user_favorites"

    id = Column(String(36), primary_key=True, index=True)
    user_id = Column(String(36), ForeignKey("users.id"), nullable=False)
    node_id = Column(String(36), nullable=False)
    create_time = Column(DateTime, default=datetime.utcnow)

    # 关系
    user = relationship("User", back_populates="favorites")

class UserHistory(Base):
    __tablename__ = "user_history"

    id = Column(String(36), primary_key=True, index=True)
    user_id = Column(String(36), ForeignKey("users.id"), nullable=False)
    node_id = Column(String(36), nullable=False)
    create_time = Column(DateTime, default=datetime.utcnow)

    # 关系
    user = relationship("User", back_populates="history") 